<?php

class IpLocation {

	private $ipdatafile = '';

	public function __construct($filename = "utfwry.dat") {
		$this->ipdatafile = $filename;
	}

	function getAddrName($ip) {

		if( !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
			return 'IANA';
		}

		if( !$fopen = @fopen($this->ipdatafile, 'rb') ) {
			return 'IANA';
		}

		$ip = explode('.', $ip);
		$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
		if( !($DataBegin = fread($fopen, 4)) || !($DataEnd = fread($fopen, 4)) ) {
			return;
		}

		@$ipbegin = implode('', unpack('L', $DataBegin));
		if($ipbegin < 0) $ipbegin += pow(2, 32);
		@$ipend = implode('', unpack('L', $DataEnd));
		if($ipend < 0) $ipend += pow(2, 32);
		$ipAllNum = ($ipend - $ipbegin) / 7 + 1;

		$BeginNum = $ip2num = $ip1num = 0;
		$ipAddr1 = $ipAddr2 = '';
		$EndNum = $ipAllNum;

		while($ip1num > $ipNum || $ip2num < $ipNum) {

			$Middle= intval(($EndNum + $BeginNum) / 2);

			fseek($fopen, $ipbegin + 7 * $Middle);
			$ipData1 = fread($fopen, 4);
			if(strlen($ipData1) < 4) {
				fclose($fopen);
				return 'IANA';
			}

			$ip1num = implode('', unpack('L', $ipData1));
			if($ip1num < 0) $ip1num += pow(2, 32);

			if($ip1num > $ipNum) {
				$EndNum = $Middle;
				continue;
			}

			$DataSeek = fread($fopen, 3);
			if(strlen($DataSeek) < 3) {
				fclose($fopen);
				return 'IANA';
			}

			$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
			fseek($fopen, $DataSeek);
			$ipData2 = fread($fopen, 4);
			if(strlen($ipData2) < 4) {
				fclose($fopen);
				return 'IANA';
			}

			$ip2num = implode('', unpack('L', $ipData2));
			if($ip2num < 0) $ip2num += pow(2, 32);

			if($ip2num < $ipNum) {
				if($Middle == $BeginNum) {
					fclose($fopen);
					return 'IANA';
				}
				$BeginNum = $Middle;
			}

		}

		$ipFlag = fread($fopen, 1);
		if($ipFlag == chr(1)) {
			$ipSeek = fread($fopen, 3);
			if(strlen($ipSeek) < 3) {
				fclose($fopen);
				return 'IANA';
			}
			$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
			fseek($fopen, $ipSeek);
			$ipFlag = fread($fopen, 1);
		}

		if($ipFlag == chr(2)) {
			$AddrSeek = fread($fopen, 3);
			if(strlen($AddrSeek) < 3) {
				fclose($fopen);
				return 'IANA';
			}
			$ipFlag = fread($fopen, 1);
			if($ipFlag == chr(2)) {
				$AddrSeek2 = fread($fopen, 3);
				if(strlen($AddrSeek2) < 3) {
					fclose($fopen);
					return 'IANA';
				}
				$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
				fseek($fopen, $AddrSeek2);
			} else {
				fseek($fopen, -1, SEEK_CUR);
			}

			while(($char = fread($fopen, 1)) != chr(0))
			$ipAddr2 .= $char;

			$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
			fseek($fopen, $AddrSeek);

			while(($char = fread($fopen, 1)) != chr(0))
			$ipAddr1 .= $char;
		} else {
			fseek($fopen, -1, SEEK_CUR);
			while(($char = fread($fopen, 1)) != chr(0))
			$ipAddr1 .= $char;

			$ipFlag = fread($fopen, 1);
			if($ipFlag == chr(2)) {
				$AddrSeek2 = fread($fopen, 3);
				if(strlen($AddrSeek2) < 3) {
					fclose($fopen);
					return 'IANA';
				}
				$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
				fseek($fopen, $AddrSeek2);
			} else {
				fseek($fopen, -1, SEEK_CUR);
			}
			while(($char = fread($fopen, 1)) != chr(0))
			$ipAddr2 .= $char;
		}
		fclose($fopen);

		if(preg_match('/http/i', $ipAddr2)) {
			$ipAddr2 = '';
		}

		$ipaddr = preg_replace('/CZ88\.NET/is', '', $ipAddr1);
		$ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
		$ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
		if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
			$ipaddr = 'IANA';
		}

		$ipaddr = iconv('GB2312', 'UTF-8//IGNORE', $ipaddr);
		return $ipaddr;
	}

}